function saveData() {
	var form = baseJs.getForm("addForm");
	if (!form.isVal()) {
		alert("请按要求填写数据！");
		return;
	}
	var json = form.getItems();
	json["parentId"] = $("#parentId").combotree("getValue");
	json["buttons"] = getButtons();
	json["operations"] = getOpers();
	json["clicks"] = getClick();
	$.post(host + "moduleAction/saveModule.html", json, function(data) {
		if (data.status == "ok") {
			baseJs.getOpenWindow().alert(data.msg);
			baseJs.getOpenWindow().datagrid.treegrid('reload');
			baseJs.getOpenWindow().dialog.close();
		} else {
			alert(data.msg);
		}
	}, "json");
}
$(function() {
	var id = baseJs.request("id");
	initModuleTree();
	if (id) {
		initPageInfo(id);
	} else {
		changeType("list");
	}
});
function initModuleTree() {
	$.post(host + "moduleAction/getModuleList.html", {}, function(data) {
		var list = onAfterLoadTree(data.rows);
		var tree = [{id: "0", text: "根模块", children: list}];
		$('#parentId').combotree({
			data: tree,
			required: true
		});
		if (parentId) {
			try {
				setTimeout(function() {
					$("#parentId").combotree("setValue", parentId);
				},400);
			} catch (e) {
			}
		}
	}, "json");
}
function onAfterLoadTree(data) {
	if (data) {
		for (var i = 0;i < data.length;i ++) {
			data[i].text = data[i].name;
			onAfterLoadTree(data[i].children);
		}
	}
	return data;
}
var parentId = null;
function initPageInfo(id) {
	$("#id").val(id);
	$.post(host + "moduleAction/getModuleInfoById.html", {id: id}, function(data) {
		if (data.status == "error") {
			alert(data.msg);
		} else {
			data = data.data;
			for (var key in data) {
				if (key == "parentId") {
					parentId = data[key];
					try {
						$("#" + key).combotree("setValue", parentId);
					} catch(e) {}
				} else {
					$("#" + key).val(data[key]);
				}
			}
			initPageButtonToPanel(data["type"], data["buttons"]);
			initPageOperToPanel(data["type"], data["operations"]);
			initClickToPanel(data["type"], data["clicks"]);
			changeType(data["type"]);
		}
	}, "json");
}
function closeWin() {
	baseJs.getOpenWindow().dialog.close();
}